import { onMounted, computed, onUnmounted, watch } from 'vue'
import useCommonStore from '@/store/common'
import { BaseEventEnum, KeyEventEnum } from '@/enums'

export function useLock() {

  let timer: number | null = null

  let lockTime = computed({
    get() {
      return useCommonStore().lockTime
    },
    set(lockTime: number) {
      useCommonStore().updatelockTime(lockTime)
    }
  })

  let isLock = computed({
    get() {
      return useCommonStore().isLock
    },
    set(isLock: boolean) {
      useCommonStore().updateisLock(isLock)
    }
  })

  watch(() => lockTime.value, () => {
    if (lockTime.value >= Number(import.meta.env.VITE_APP_LOCK_TIME) * 60) {
      isLock.value = true
    } else {
      if(isLock.value) {
        isLock.value = false
      }
    }
  })

  watch(() => isLock.value, () => {
    if(isLock.value) {
      removeHandle()
    } else {
      init()
    }
  })

  onMounted(() => {
    if(isLock.value) {
      lockTime.value = Number(import.meta.env.VITE_APP_LOCK_TIME) * 60
    } else {
      init()
    }
  })

  onUnmounted(() => {
    removeHandle()
  })

  function init() {
    reckonByTime()
    document.addEventListener(BaseEventEnum.ON_CLICK, clickHandle)
    document.addEventListener(BaseEventEnum.ON_DBL_CLICK, dblclickHandle)
    document.addEventListener(BaseEventEnum.ON_CONTEXTMENU, contextmenuHandle)
    document.addEventListener(BaseEventEnum.ON_MOUSE_MOVE, mousemoveHandle)
    document.addEventListener(BaseEventEnum.ON_MOUSE_WHEEL, mousewheelHandle)
    document.addEventListener(KeyEventEnum.ON_KEY_DOWN, keydownHandle)
  }

  function removeHandle() {
    if (timer) {
      clearInterval(timer)
      timer = null
    }
    document.removeEventListener(BaseEventEnum.ON_CLICK, clickHandle)
    document.removeEventListener(BaseEventEnum.ON_DBL_CLICK, dblclickHandle)
    document.removeEventListener(BaseEventEnum.ON_CONTEXTMENU, contextmenuHandle)
    document.removeEventListener(BaseEventEnum.ON_MOUSE_MOVE, mousemoveHandle)
    document.removeEventListener(BaseEventEnum.ON_MOUSE_WHEEL, mousewheelHandle)
    document.removeEventListener(KeyEventEnum.ON_KEY_DOWN, keydownHandle)
  }

  // 无操作计时
  function reckonByTime() {
    if (timer) {
      lockTime.value = 0
      clearInterval(timer)
      timer = null
    }
    timer = setInterval(() => {
      lockTime.value += 1
    }, 1000)
  }

  // 鼠标点击
  function clickHandle() {
    reckonByTime()
  }

  // 鼠标双击
  function dblclickHandle() {
    reckonByTime()
  }

  // 鼠标右键
  function contextmenuHandle() {
    reckonByTime()
  }

  // 鼠标移动
  function mousemoveHandle() {
    reckonByTime()
  }

  // 鼠标滚轮
  function mousewheelHandle() {
    reckonByTime()
  }
  
  // 键盘按下
  function keydownHandle(event: KeyboardEvent) {
    const { altKey, ctrlKey, key } = event
    if (altKey && ctrlKey && key == 'l') {
      // Ctrl + Alt + L 快捷键直接锁屏
      lockTime.value = Number(import.meta.env.VITE_APP_LOCK_TIME) * 60
    } else {
      reckonByTime()
    }
  }

  return isLock
}